package com.hsd.controller;

import com.hsd.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class TestController {

    /**
     * RequestMapping 定义当前controller中当前方法的映射路径：
     * http://192.168.0.110:8080/映射路径
     *
     * @return
     */
    @RequestMapping("test/helloWorld")
    public void helloWorld() {
        System.out.println("hello world!");
    }

    /**
     * 使用ModelAndView向视图层传参
     *
     * @return
     */
    @RequestMapping("test/sendToView")
    public ModelAndView sendToJSP() {
        String data = "ModelAndView携带参数而来！";
        Map<String, String> map = new HashMap<>();
        map.put("msg", data);
        ModelAndView modelAndView = new ModelAndView("../index.jsp", map);
        return modelAndView;
    }

    /**
     * 使用ModelMap向视图层传参
     *
     * @return
     */
    @RequestMapping("test/sendToView2")
    public String sendToJSP2(ModelMap modelMap) {
        String data = "ModelMap携带参数而来！";
        modelMap.put("msg", data);
        return "forward:../index.jsp";
    }

    /**
     * 获取前端传来的基本类型的参数
     *
     * @param name
     * @param sex
     * @param age
     */
    @RequestMapping("test/sendParam")
    public void getParamFromView(String name, String sex, Integer age) {
        System.out.println("姓名：" + name);
        System.out.println("性别：" + sex);
        System.out.println("年龄：" + age);
    }

    /**
     * 获取前端传来的参数并封装成对象【对象类型的传输】
     *
     * @param user
     */
    @RequestMapping("test/sendObj")
    public void getParamFromView2(User user) {
        System.out.println(user);
    }

    /**
     * 获取前端传来的集合参数
     *
     * @param list
     */
    @RequestMapping("test/sendList")
    public void getParamFromView3(@RequestParam("list") List<String> list) {
        for (String s : list) {
            System.out.println(s);
        }
    }
}
